package com.wuxiaoge.service;

import com.wuxiaoge.dao.UserRepository;
import com.wuxiaoge.entity.User;
import com.wuxiaoge.exception.BusinessException;
import com.wuxiaoge.result.ResultCodeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {

    private final UserRepository userRepository;

    @Transactional
    public int updateUserStatus(String name, Long userId){
        return userRepository.updateUserStatus(name,userId);
    }

    public User save(User user) {

        if(StringUtils.isBlank(user.getName())){
            throw new BusinessException(ResultCodeEnum.PARAM_MISSING_ERROR.getCode(),ResultCodeEnum.PARAM_MISSING_ERROR.getMessage());
        }

        if("张三".equals(user.getName())){
            throw new BusinessException(ResultCodeEnum.PARAM_ERROR.getCode(),ResultCodeEnum.PARAM_ERROR.getMessage());
        }

//        int i = 1/0;

//        user = null;
        System.out.println(user.getName());

        return userRepository.save(user);

    }
}
